/**
 * COPYRIGHT© KMTC 2007
 * This software is the proprietary information of Korea Marine Transport Co.LTD
 * 
 * Revision History
 * Author			Date		Description
 * ------			----		-----------
 * 김주희@wees.co.kr		
 */
package kr.or.javacafe.common.web;

import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.Enumeration;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author mousing
 *
 */
public class MyUrlFilenameViewController implements Controller{
	
	protected final Log logger = LogFactory.getLog(getClass());
	
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) {
		
		String contextPath = request.getContextPath();
		String uri = request.getRequestURI();
		
		int begin = 0;
		if ((contextPath == null) || (contextPath.equals(""))) {
			begin = 1;
		} else {
			begin = contextPath.length() + 1;
		}
		
		int end;
		if (uri.indexOf(";") != -1) {
			end = uri.indexOf(";");
		} else if (uri.indexOf("?") != -1) {
			end = uri.indexOf("?");
		} else {
			end = uri.length();
		}
		
		String fileName = uri.substring(begin, end);
		if (fileName.indexOf(".") != -1) {
			fileName = fileName.substring(0, fileName.lastIndexOf("."));
		}

		for (Enumeration en = request.getParameterNames(); en.hasMoreElements();) {
			String attribute = (String) en.nextElement();
			Object attributeValue = request.getParameter(attribute);

			if (logger.isDebugEnabled()) {
				logger.debug("set Attribute in Request : " + attribute + "="
						+ attributeValue);
			}

			request.setAttribute(attribute, attributeValue);
		}
		
		logger.debug("######## fileName: "+fileName);

		return new ModelAndView(fileName);
	}
	
}
